﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SSharp.Core.DataTypes;
using SSharp.Core.Evaluator;

namespace SSharp.Core.Builtins.Functions {
	public static class PrimitiveBoolean {
		public static readonly PrimitiveProcedure Not = new PrimitiveProcedure("not",
			args => {
				if (args.Length != 1) {
					throw new ApplicationError("not expects one argument, given " + args.Length);
				} else {
					bool? b = args[0] as bool?;
					if (b == null) {
						throw new ArgumentException("not expects boolean argument, given " + args[0]);
					}

					return !(b.Value);
				}
			});

		public static readonly PrimitiveProcedure And = new PrimitiveProcedure("and",
			args => {
				if (args.Length < 1) {
					throw new ApplicationError("and expects at least one argument, given " + args.Length);
				} else {
					foreach (object arg in args) {
						bool? b = arg as bool?;
						if (b == null) {
							throw new ArgumentException("and expects boolean arguments, given " + arg);
						}
						if (!b.Value) {
							return false;
						}
					}
					return true;
				}
			});

		public static readonly PrimitiveProcedure Or = new PrimitiveProcedure("or",
			args => {
				if (args.Length < 1) {
					throw new ApplicationError("or expects at least one argument, given " + args.Length);
				} else {
					foreach (object arg in args) {
						bool? b = arg as bool?;
						if (b == null) {
							throw new ArgumentException("or expects boolean arguments, given " + arg);
						}
						if (b.Value) {
							return true;
						}
					}
					return false;
				}
			});
	}
}
